﻿
using B.S.Traceability.Domain;
using B.S.Traceability.Error;
using B.S.Traceability.Infrastructure.Interface;
using B.S.Traceability.Writer.API.Application.Commends;
using MediatR;

namespace B.S.Traceability.Writer.API.Application.CommendHandlers
{
    public class DelProductsCommendHandlers : IRequestHandler<DelProductsCommends, APIResult<Products>>
    {
        private readonly IBaseRepository<Products> productsRepository;

        public DelProductsCommendHandlers(IBaseRepository<Products> productsRepository)
        {
            this.productsRepository = productsRepository;
        }

        /// <summary>
        /// 逻辑删除产品信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<Products>> Handle(DelProductsCommends request, CancellationToken cancellationToken)
        {
            APIResult<Products> result = new APIResult<Products>();
            result.Code = APIEnum.删除成功;
            result.Msg= "删除成功";

            var product = productsRepository.GetAllAsync().Result.FirstOrDefault(p=>p.ProductsId==request.ProductsId);
             product.IsDel = !product.IsDel;

            await productsRepository.Update(product);


            return await Task.FromResult(result);
        }
    }
}
